package com.lul15834.orderfood.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.lul15834.orderfood.R;
import com.lul15834.orderfood.activity.manager.Manager;
import com.lul15834.orderfood.activity.manager.UpdatePwd;
import com.lul15834.orderfood.dao.AdminDao;
import com.lul15834.orderfood.until.Tools;

public class UserUpdatePwd extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_user_update_pwd);
        Toolbar toolbar=findViewById(R.id.update_userPwd_back);
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(UserUpdatePwd.this, ManageUser.class);
                intent.putExtra("sta","1");
                startActivity(intent);
            }
        });
        EditText pwd=findViewById(R.id.update_user_pwd);
        EditText confirm=findViewById(R.id.update_user_pwd1);
        Button btn=findViewById(R.id.update_userPwd_btn);
        String account= Tools.getOnAccount(this);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pwdT=pwd.getText().toString().trim();
                String confirmT=confirm.getText().toString().trim();
                if (pwdT.isEmpty()){
                    pwd.setError("请输入密码");
                    pwd.requestFocus();
                }else if (confirmT.isEmpty()){
                    confirm.setError("请输入确认密码");
                    confirm.requestFocus();
                }else if (!pwdT.equals(confirmT)){
                    confirm.setError("两次密码不一致，请重新输入");
                    confirm.requestFocus();
                }else {
                    int a= AdminDao.UpdateUserPwd(account,pwdT);
                    if (a==1){
                        Toast.makeText(UserUpdatePwd.this,"更改成功",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(UserUpdatePwd.this,"更改失败",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}